package itsm.isperp.module.controller.app;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.domain.IUser;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.app.AppCommonSource;
import itsm.isperp.module.service.app.AppCommonSourceService;
import itsm.isperp.module.service.project.ProjProjectService;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 控制器
 * 
 * @author lizx
 * @date 2017-03-08 10:34
 */
@Controller
@RequestMapping("app/commonSource")
public class AppCommonSourceController extends BaseController<AppCommonSource> {

  @Autowired
  protected AppCommonSourceService appCommonSourceService;

  @Override
  public AppCommonSourceService getService() {
    return this.appCommonSourceService;
  }

  @Override
  public String getPageTitle() {
    return "配置";
  }

  @RequestMapping(value = { "providerUserEdit" })
  public ModelAndView providerUserEdit(HttpServletRequest req, String id) {
    ModelAndView mv = this.getModelAndView();
    AppCommonSource model = null;
    String sourceId = req.getParameter("sourceId");
    if (StringUtils.isNotBlank(id)) {
      model = appCommonSourceService.findOne(id);
    } else {
      model = new AppCommonSource();
      model.setId(HibernateUUIDGenerator.generate());
      if (StringUtils.isNotBlank(sourceId))
        model.setSourceId(sourceId);
    }
    mv.addObject("model", model);

    return mv;

  }
  
  @RequestMapping(value = { "providerAppraiseEdit" })
  public ModelAndView providerAppraiseEdit(HttpServletRequest req, String id) {
    ModelAndView mv = this.getModelAndView();
    AppCommonSource model = null;
    String sourceId = req.getParameter("sourceId");
    if (StringUtils.isNotBlank(id)) {
      model = appCommonSourceService.findOne(id);
    } else {
      IUser user = ContextHolder.getLoginUser();
      model = new AppCommonSource();
      model.setDateValueA(new Date());
      model.setNameA(user.getFullname());
      model.setId(HibernateUUIDGenerator.generate());
      if (StringUtils.isNotBlank(sourceId))
        model.setSourceId(sourceId);
    }
    ProjProjectService projProjectService=ContextHolder.getSpringBean("projProjectService");
    mv.addObject("projects",
        projProjectService.findAllPublished("id", "name"));
    mv.addObject("model", model);

    return mv;

  }
  
  @RequestMapping("listProUsers")
  @ResponseBody
  public PageResponse<AppCommonSource> listProUsers(JqGridRequest request,
      HttpServletRequest req) {
    String sourceId=req.getParameter("sourceId");
    if(StringUtils.isNotBlank(sourceId))
    {
      request.addFilterRule("source_id", JqGridFilterRuleOp.eq, sourceId);
      request.addFilterRule("source_type", JqGridFilterRuleOp.eq, "providerUser");
    }
      
    return this.getService().findAll(request).toPage();
  }
  
  @RequestMapping("listProAppraise")
  @ResponseBody
  public PageResponse<AppCommonSource> listProAppraise(JqGridRequest request,
      HttpServletRequest req) {
    String sourceId=req.getParameter("sourceId");
    if(StringUtils.isNotBlank(sourceId))
    {
      request.addFilterRule("source_id", JqGridFilterRuleOp.eq, sourceId);
      request.addFilterRule("source_type", JqGridFilterRuleOp.eq, "providerAppraise");
    }
      
    return this.getService().findAll(request).toPage();
  }
  
  @RequestMapping("updateProUser")
  @ResponseBody
  public String updateProUser(JqGridRequest request,
      HttpServletRequest req) {
    String id=req.getParameter("id");
    String sourceId=req.getParameter("sourceId");
    String linkman=req.getParameter("linkman");
    String linktel=req.getParameter("linktel");
    JdbcTemplate jdbcTemplate=ContextHolder.getSpringBean("jdbcTemplate");
    String sql1="update app_common_source set value_num_a=0 where name_f='"+sourceId+"'";
    String sql2="update app_common_source set value_num_a=1 where id='"+id+"'";
    String sql3="UPDATE prd_provider  SET linkman='"+linkman+"',linktel='"+linktel+"' WHERE id='"+sourceId+"'";
    jdbcTemplate.batchUpdate(sql1,sql2,sql3);
    return "";
  }
}
